package com.hujiang.basic.search.settings;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hujiang.basic.framework.rest.validation.annotation.NotBlank;
import com.hujiang.basic.framework.rest.validation.annotation.NotNull;
import com.hujiang.basic.search.handler.impl.model.BuildIndexRequest;
import com.hujiang.basic.search.model.HJResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.List;

@RestController
@Api("设置ES API")
@RequestMapping(value = "/v1/settings")
@Slf4j
public class SettingsContorller {
    
    @Autowired
    private SettingsService settingsService;
    
    @ApiOperation(value = "创建索引", httpMethod = "POST", response = HJResponse.class, notes = "创建索引post接口")
    @RequestMapping(value = "/index/build", method = RequestMethod.POST)
    @NotNull("req")
    @NotBlank({ "req.indexConifgFileName" })
    public HJResponse buildIndex(@RequestBody BuildIndexRequest req)  {
        String indexName = null;
        try {
            indexName = this.settingsService.buildIndex(req);
        } catch (IOException e) {
            log.error("build index error:{}",e);
            return HJResponse.fail(e.getMessage());
        }
        return HJResponse.ok(indexName);
    }
    
    @ApiOperation(value = "得到索引配置列表", httpMethod = "GET", response = HJResponse.class, notes = "得到索引配置列表get接口")
    @RequestMapping(value = "/index/listconfigs", method = RequestMethod.GET)
    public HJResponse getIndexConfigs() {
        List<String> indexConfigs =null;
        try {
            indexConfigs = this.settingsService.getIndexConfigFileNames();
        } catch (IOException e) {
            log.error("getIndexConfigs error:{}",e);
            return HJResponse.fail(e.getMessage());
        }
        return HJResponse.ok(indexConfigs);
    }
}
